package com.lyy.mapper;

import com.lyy.entity.Chapter;
import com.lyy.entity.Episode;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Liangyaoyun
 */
@Repository
public interface ChapterMapper {
    String list = "select c.chapter_id, c.chapter_title, c.chapter_info, c.chapter_index, c.fk_video_id, " +
            "c.create_time, c.modify_time from lesson.chapter c";

    /**
     * 按条件查询章记录，分步联查该章下的全部集记录
     *
     * @param condition 查询条件
     * @return 查询失败时返回空List对象，结果集按 chapter_index 升序，chapter_index 相同时按创建时间降序
     */
    @Results(id = "withEpisodes", value = {
            @Result(id = true, column = "chapter_id", property = "chapterId"),
            @Result(column = "chapter_id", property = "episodes",
                    many = @Many(select = "com.lyy.mapper.EpisodeMapper.selectByChapterId"))
    })
    @Select("<script>" + list +
            "<where> " +
            "<if test='chapterId != null'>and c.chapter_id = #{chapterId}</if> " +
            "<if test='chapterTitle != null'>and c.chapter_title = #{chapterTitle}</if> " +
            "<if test='chapterInfo != null'>and c.chapter_info = #{chapterInfo}</if> " +
            "<if test='chapterIndex != null'>and c.chapter_index = #{chapterIndex}</if> " +
            "<if test='fkVideoId != null'>and c.fk_video_id = #{fkVideoId}</if> " +
            "<if test='createTime != null'>and c.create_time = #{createTime}</if> " +
            "<if test='modifyTime != null'>and c.modify_time = #{modifyTime}</if> " +
            "</where> " +
            "order by c.chapter_index, create_time desc " +
            "</script> ")
    List<Chapter> select(Chapter condition);

    /**
     * 按视频表主键查询该视频下的全部章记录（分步调用专用）
     *
     * @param videoId 视频表主键
     * @return 查询失败时返回空List对象，结果集按 chapter_index 升序，chapter_index 相同时按创建时间降序
     */
    @ResultMap("withEpisodes")
    @Select(list + " where c.fk_video_id = #{param1} order by c.chapter_index, create_time desc ")
    List<Chapter> selectByVideoId(Integer videoId);











    /*  *//**
     * 按Chapter主键查询Episode记录（分步调用专用SQL块）
     *
     * @param chapterId Chapter表主键
     * @return 多条Episode记录，结果集按 index_in_chapter 升序
     *//*
  @Select("select * from lesson.chapter " + " where e.fk_chapter_id = #{param1} order by e.index_in_chapter")
  List<Episode> selectByChapterId(Integer chapterId);

  *//**
     * 查询Chapter记录
     *
     * @param chapter Chapter实体
     * @return 多条Chapter记录，包含Episode记录
     *//*
  @Results({
          @Result(id = true, column = "chapter_id", property = "chapterId"),
          @Result(column = "chapter_id", property = "episodeList",
                  many = @Many(select = "com.lyy.mapper.EpisodeMapper.selectByChapter"))
  })
  @Select("<script>" +
          "select c.chapter_id, c.chapter_title, c.chapter_info, c.fk_video_id, " +
          "c.index_in_video, c.create_time, c.modify_time " +
          "from lesson.chapter c" +
          "<where>" +
          "<if test='chapterId != null'>and chapter_id = #{chapterId}</if>" +
          "<if test='chapterTitle != null'>and chapter_title = #{chapterTitle}</if>" +
          "<if test='chapterInfo != null'>and chapter_info = #{chapterInfo}</if>" +
          "<if test='fkVideoId != null'>and fk_video_id = #{fkVideoId}</if>" +
          "<if test='indexInVideo != null'>and index_in_video = #{indexInVideo}</if>" +
          "<if test='createTime != null'>and create_time = #{createTime}</if>" +
          "<if test='modifyTime != null'>and modify_time = #{modifyTime}</if>" +
          "</where>" +
          "order by c.index_in_video" +
          "</script>")
  List<Chapter> select(Chapter chapter);




    *//*一对多分步查询按照chapter_id来链接chapter表跟episode表*//*
    //property对应的是Java中的实体类属性，column对应数据库里面的属性
    @Results({
            //第一条@Result是为了加速用的，可以不配
            @Result(id = true, property = "chapterId", column = "chapter_id"),
            //先执行的是@Select里面的语句，根据fk_video_id查到一堆chapter，
            // 然后根据chapter_id执行com.lyy.mapper.EpisodeMapper.selectByChapter包里的语句
            @Result(property = "episodeList", column = "chapter_id",
                    many = @Many(select = "com.lyy.mapper.EpisodeMapper.selectByChapter"))
    })
    *//*根据videoId查询到chapter表里的数据*//*
    @Select("select * from lesson.chapter where fk_video_id=#{param1} order by index_in_video")
    List<Chapter> selectByVideoId(Integer videoId);

    @Insert("insert into lesson.chapter(chapter_title,chapter_info,fk_video_id,index_in_video,create_time,modify_time)value(#{chapterTitle},#{chapterInfo},#{fkVideoId},#{indexInVideo},#{createTime},#{modifyTime})")
    int insertChapter(Chapter chapter);

    String updateChapter = "<script> update lesson.chapter <set> " +
            "<if test='chapterTitle!=null'>chapter_title=#{chapterTitle},</if>" +
            "<if test='chapterInfo!=null'>chapter_info=#{chapterInfo},</if>" +
            "<if test='fkVideoId!=null'>fk_video_id=#{fkVideoId},</if>" +
            "<if test='indexInVideo!=null'>index_in_video=#{indexInVideo},</if>" +
            "<if test='modifyTime!=null'>modify_time=#{modifyTime},</if>" +
            "</set>" +
            "where chapter_id=#{chapterId}" +
            "</script>";

    @Update(updateChapter)
    int updateByChapterId(Chapter chapter);

    *//*删除*//*
    @Delete("delete from lesson.chapter where chapter_id=#{param1}")
    int deleteByChapterId(Integer chapterId);*/
}
